package com.cskaoyan.market.controller;

import com.cskaoyan.market.db.domain.MarketLog;
import com.cskaoyan.market.service.AdminLogeService;
import com.cskaoyan.market.service.impl.AdminLogServiceImpl;
import com.cskaoyan.market.util.JacksonUtil;
import com.cskaoyan.market.util.ResponseUtil;
import com.fasterxml.jackson.core.JsonProcessingException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @author:yanzijun
 * @time:2024/06/21/9:03
 */
@WebServlet("/admin/log/*")
public class AdminLogController extends HttpServlet {
    AdminLogeService adminLogeService = new AdminLogServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String requestURI = req.getRequestURI();
        String replaced = requestURI.replace(req.getContextPath() + "/admin/log/", "");
        if ("list".equals(replaced)) {
            list(req, resp);
        }
    }

    private void list(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String page = req.getParameter("page");
        String limit = req.getParameter("limit");
        String name = req.getParameter("name");
        String sort = req.getParameter("sort");
        String order = req.getParameter("order");
        List<MarketLog> marketLogs = adminLogeService.list(page, limit, name, sort, order);
        Object o = ResponseUtil.okList(marketLogs);
        String s = JacksonUtil.getObjectMapper().writeValueAsString(o);
        resp.getWriter().println(s);
    }
}
